<?php

namespace app\mini\controller;

use think\facade\Db;

class Address extends Access
{
    /**
     * 获取客户的地址信息
     *
     */
    public function info()
    {
        $customer_id = $this->customerId; // 用户登录成功后获取的账户id
        if (!$customer_id) {
            return resultArray(['error' => '请先绑定签约码']);
        }
        $field = ['bind_id', 'contacter', 'telephone', 'province', 'city', 'district', 'address', 'create_time'];
        $list = Db::name('crm_customer_bind')
            ->where('customer_id', $customer_id)
            ->field($field)
            ->select()->toArray();

        // 处理省市区返回数据
        $region = Db::name('shop_region')->column('name', 'id');
        foreach ($list as &$value) {
            $value['province_name'] = $value['province'] ? $region[$value['province']] : '';
            $value['city_name'] = $value['city'] ? $region[$value['city']] : '';
            $value['district_name'] = $value['district'] ? $region[$value['district']] : '';
        }

        return resultArray(['data' => $list, 'error' => '获取成功']);
    }
}